<template>
  <div o-Container>
    <o-list v-bind:source="list.mqttsource" v-bind="{created:create,modified:modify,exported:output}"/>

    <o-dialog v-bind:source="panel.source" v-bind="{opened:loadPanel,closed:loadPanel}">
      <template slot="content">
        <o-item v-bind:source="item.mqttsource"
                v-bind="{loaded:load,saved:save,validated:validate,backed:back}"></o-item>
        <o-list v-bind:source="list.mqttkey" v-bind="{created:createKey,modified:modifyKey}"/>
      </template>
    </o-dialog>

    <o-dialog v-bind:source="panel.key" v-bind="{opened:loadPanel,closed:loadPanel}">
      <template slot="content">
        <o-item v-bind:source="item.mqttkey"
                v-bind="{loaded:loadKey,saved:saveKey,validated:validateKey,backed:backKey}"></o-item>
      </template>
    </o-dialog>
  </div>
</template>

<script>
  export default {
    name: "mms_engine_mqtt_list", /* 这个name暂时不知道用啥用，根据官方文档说的是方便排错的 */
    data() {
      return {
        octopus: this.$parent.octopus,
        //列表 数据
        list: {
          mqttsource: {
            data: {
              id: 625,
              cellData: [
                {name: "Name", searchKey: "LIKE", width: 200},
                {name: "ServerID", searchKey: "=", width: 150, link: [{mapping: "TypeID", value: 8}]},// 只加载MQTT服务器
                {name: "Path", searchKey: "LIKE", width: 470},
                {name: "DataTypeID", searchKey: "="},
                {name: "TargetTableID", searchKey: "=", width: 200},
                // {name: "Expression", searchKey: "LIKE", width: 150},
                // {name: "TableID", width: 200},
                {name: "Enable", searchKey: "=", width: 60},
                {name: "Note", width: 200},
                {name: "CreateDate", width: 160},
              ],
            },
          },
          mqttkey: {
            data: {
              id: 742,
              cellData: [
                {name: "MqttID", filterKey: "=", show: false},
                {name: "ContainKey", width: 150},
                {name: "Extract", width: 270},
                {name: "Expression", width: 150},
                // {name: "TargetTableID", width: 200},
                {name: "TargetColumnID", width: 200},
                {name: "Note", width: 300},
                {name: "CreateDate", width: 160},
              ],
              height: "200px",
            },
          },
        },
        // 创建页面和修改页面的需要的数据
        item: {
          mqttsource: {
            data: {
              id: 625,
              value: "",
              cellData: [
                {name: "Name"},
                {name: "Path"},
                {name: "ServerID", link: [{mapping: "TypeID", value: 8}]},
                {name: "DataTypeID"},
                {name: "TargetTableID"},
                {name: "Enable"},
                // {name: "CreateDate"},
                {name: "Note"},
              ],
            }
          },
          mqttkey: {
            data: {
              id: 742,
              cellData: [
                {name: "MqttID", show: false},
                {name: "ContainKey"},
                {name: "Extract"},
                {name: "Expression"},
                // {name: "TargetTableID"},
                {name: "TargetColumnID", link: [{mapping: "TableID", value: 0}]},
                {name: "CreateDate"},
                {name: "Note"},
              ],
            },
          },
        },
        panel: {
          source: {
            visible: false
          },
          key: {
            visible: false
          }
        },
        mqttID: null,
      }
    },
    created: function () {
      this.init();
    },
    methods: {
      // 适配 平台 和 用户
      init: function () {

      },
      load: function (data) {
        // alert(JSON.stringify(data));
        // 得到TableID对象
        let ListFilterData = $.grep(this.list.mqttkey.data.cellData, function (value) {
          return value.name == "MqttID";//筛选出消息源ID编号
        })[0];
        // 如果Menu有主键值，说明是更新动作，否则创建动作，没有的加了也白加。
        if (this.item.mqttsource.data.value) {
          // 定义TableID具体内容
          ListFilterData.filterValue = this.item.mqttsource.data.value;
        } else {
          // 新创建页面时默认-1，查不到，最牛逼。
          ListFilterData.filterValue = -1;
        }
        this.list.mqttkey.control.load();

        // 更新最新更新人
        // this.octopus.tools.parseCell(this.item.datasource.data, "UpdateDate").defaultValue =  this.octopus.tools.date("yyyy-MM-dd HH:mm:ss");
        // this.octopus.tools.parseCell(this.item.datasource.data, "UpdaterID").defaultValue =  this.octopus.passport.id.toString();
      },
      validate: function (status) {
        let pathData = this.octopus.tools.parseCell(this.item.mqttsource.data, "Path").defaultValue;
        if (pathData.indexOf("#{sensor}") == -1) {
          this.octopus.tips.error("路径必须带#{sensor}表达式，否则引擎无法解析。");
          // 将表达式设置为false
          status = false;
        }

        return status;
      },
      loadPanel: function (data) {
        //
      },
      create: function () {
        this.item.mqttsource.data.value = null;
        this.item.mqttsource.control.load();
        this.panel.source.visible = true;
      },
      modify: function (row) {
        this.mqttID = row.ID;
        this.item.mqttsource.data.value = row.ID;
        this.item.mqttsource.control.load();
        this.panel.source.visible = true;
      },
      save: function (data) {
        this.octopus.tips.success(data);
        // 刷新list
        this.list.mqttsource.control.load();
        this.back();
      },
      back: function () {
        this.panel.source.visible = false;
      },
      output: function (data) {
        this.octopus.tips.success("下载完成。");
      },


      loadKey: function (data) {
        // 如果是创建 value为空 并且 归属 mqttID 不为空；
        if (this.octopus.tools.IsNullOrEmpty(data.content.value) && !this.octopus.tools.IsNullOrEmpty(this.mqttID)) {
          this.octopus.tools.parseCell(data.content, "MqttID").defaultValue = this.mqttID.toString();
        }
      },
      validateKey: function (status) {
        // let expressionData = this.octopus.tools.parseCell(this.item.mqttkey.data, "Expression").defaultValue;
        // if (!this.octopus.tools.IsNullOrEmpty(expressionData) && expressionData.indexOf("#{value}") == -1) {
        //   this.octopus.tips.error("表达式不为空时，必须包含#{value}表达式，否则解析引擎将无法正常工作。");
        //   // 将表达式设置为false
        //   status = false;
        // }
        return status;
      },
      createKey: function () {
        this.item.mqttkey.data.value = null;
        let targetTableID = this.octopus.tools.parseCell(this.item.mqttsource.data, "TargetTableID").defaultValue;
        targetTableID = targetTableID != "" ? targetTableID : 0;
        this.octopus.tools.parseCell(this.item.mqttkey.data, "TargetColumnID").link = [{mapping: "TableID", value: targetTableID}];

        this.item.mqttkey.control.load();
        this.panel.key.visible = true;
      },
      modifyKey: function (row) {
        this.item.mqttkey.data.value = row.ID;
        let targetTableID = this.octopus.tools.parseCell(this.item.mqttsource.data, "TargetTableID").defaultValue;
        targetTableID = targetTableID != "" ? targetTableID : 0;
        this.octopus.tools.parseCell(this.item.mqttkey.data, "TargetColumnID").link = [{mapping: "TableID", value: targetTableID}];

        this.item.mqttkey.control.load();
        this.panel.key.visible = true;
      },
      saveKey: function (data) {
        this.octopus.tips.success(data);
        // 刷新list
        this.list.mqttkey.control.load();
        this.backKey();
      },
      backKey: function () {
        this.panel.key.visible = false;
      },
    },
    props: ["oData"]
  }
</script>
